package com.ruoyi.matrix.controller.operate

import javax.servlet.http.HttpServletResponse
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.*
import com.ruoyi.common.annotation.Log
import com.ruoyi.common.core.controller.BaseController
import com.ruoyi.common.core.domain.AjaxResult
import com.ruoyi.common.enums.BusinessType
import com.ruoyi.matrix.domain.Customer
import com.ruoyi.matrix.condition.CustomerCondition
import com.ruoyi.matrix.service.CustomerService
import com.ruoyi.common.utils.poi.ExcelUtil
import com.ruoyi.common.core.page.TableDataInfo

/**
 * 客户Controller
 * 
 * @author matrix
 * @date 2023-06-08
 */
@RestController
@RequestMapping("/operate/customer")
public class CustomerOperateController(
    private val customerService: CustomerService,
) : BaseController() {

    /**
     * 查询客户列表
     */
    @PreAuthorize("@ss.hasPermi('operate:customer:list')")
    @GetMapping("/list")
    fun list(customerCondition: CustomerCondition): TableDataInfo {
        collectPageParams(customerCondition)
        val list = customerService.select(customerCondition)
        return buildDataTable(list, customerService.count(customerCondition))
    }

    /**
     * 导出客户列表
     */
    @PreAuthorize("@ss.hasPermi('operate:customer:export')")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    fun export(response: HttpServletResponse, customerCondition: CustomerCondition) {
        val list = customerService.select(customerCondition)
        val util = ExcelUtil(Customer::class.java)
        util.exportExcel(response, list, "客户数据")
    }

    /**
     * 获取客户详细信息
     */
    @PreAuthorize("@ss.hasPermi('operate:customer:query')")
    @GetMapping(value = ["/get/{id}"])
    fun getInfo(@PathVariable("id") id: Long): AjaxResult {
        return AjaxResult.success(customerService.getById(id))
    }

    /**
     * 新增客户
     */
    @PreAuthorize("@ss.hasPermi('operate:customer:add')")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    fun add(@RequestBody customer: Customer): AjaxResult {
        return toAjax(customerService.insert(customer))
    }

    /**
     * 修改客户
     */
    @PreAuthorize("@ss.hasPermi('operate:customer:edit')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    fun edit(@RequestBody customer: Customer): AjaxResult {
        return toAjax(customerService.update(customer))
    }

    /**
     * 删除客户
     */
    @PreAuthorize("@ss.hasPermi('operate:customer:remove')")
    @Log(title = "客户", businessType = BusinessType.DELETE)
	@PostMapping("/remove/{id}")
    fun remove(@PathVariable id: Long): AjaxResult {
        return toAjax(customerService.deleteById(id))
    }

    /**
     * 删除客户
     */
    @PreAuthorize("@ss.hasPermi('operate:customer:remove')")
    @Log(title = "客户", businessType = BusinessType.DELETE)
    @PostMapping("/removes")
    fun remove(@RequestBody ids: Array<Long>): AjaxResult {
        return toAjax(customerService.deleteByIds(ids))
    }
}
